<?php

namespace Admin\Controller;

class ShopController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Shop");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    /**
     * 添加数据
     */
    public function addPost() {

        $shopName = $_REQUEST["shopName"];

        $shopMap["shop_name"] = $shopName;

        $shop = $this->currentModel->queryByMap($shopMap);

        if(!isNullOrEmpty($shop)) {
            $this->error("对不起,名称重复,请重新填写");
        }

        $shopData["address"] = $_REQUEST["address"];
        $shopData["city_name"] = $shopName;
        $shopData["create_time"] = getCurrentDate();
        $shopData["contact"] = $_REQUEST["contact"];

        $this->currentModel->add($shopData);

        $this->success("添加成功", U("shop/index"));

    }

    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];

        $shop = $this->currentModel->queryById($id);

        $shopName = $_REQUEST["shopName"];

        $shopMap["shop_name"] = $shopName;
        $shopMap["shop_id"] = array("neq", $shop["shop_id"]);

        $repeatShop = $this->currentModel->queryByMap($shopMap);

        if(!isNullOrEmpty($repeatShop)) {
            $this->error("对不起,名称已经存在,请重新填写");
        }

        $shop["shop_name"] = $shopName;
        $shop["address"] = $_REQUEST["address"];
        $shop["contact"] = $_REQUEST["contact"];

        $this->currentModel->updateInfo($shop);

        $this->success("更新成功", U("shop/index"));

    }


}